// simulate cue-taking model
// show mapping of pair-level effect to general effect
// use actual Icelandic setting as far as possible
clear all
local votinganalyfolder = "$path_pch\Data\analysis\Voting"
local seatingfolder = "$path_pch\Data\intermediate\Seating"
timer on 1
set seed 562924


** Auto-fill 
local pbig = $pcue*100
local pcuelab "`pbig'perc"

** Read in neighbours in placebo RI draws
use "`seatingfolder'\seating_MP_frontback.dta", clear

local tokeep ""
forv i=$drawstart/$drawend {
	local tokeep "`tokeep' L_MP_idRI`i' U_MP_idRI`i' L_party_idRI`i' U_party_idRI`i'"
}

isid MP_id session_id
keep MP_id session_id party_id `tokeep'
tempfile neighbours
save `neighbours'


** Read in vector of real votes, leader votes
use "`votinganalyfolder'/voting_analysis", clear
keep MP_id vote_id bill_id session_id vote leader_vote
merge m:1 MP_id session_id using `neighbours', assert(2 3) keep(3) nogen
order MP_id session_id vote_id party_id vote leader_vote

foreach var of varlist vote leader_vote {
	replace `var'="3" if `var'=="yes"
	replace `var'="2" if `var'=="absent"
	replace `var'="1" if `var'=="abstain"
	replace `var'="0" if `var'=="no"
	destring `var', replace
}

lab def vote_options 0 "No" 1 "Abstain" 2 "Absent" 3 "Yes"
lab val vote vote_options
lab val leader_vote vote_options

foreach x in U L {
	forv i=$drawstart/$drawend {
		g `x'_diffparty`i' = party_id!=`x'_party_idRI`i' if !mi(party_id) & !mi(`x'_party_idRI`i')
		drop `x'_party_idRI`i'
	}
}

drop party_id
sort session_id vote_id MP_id
ren vote vote_natural

forv i=$drawstart/$drawend {
	g vote_influenced`i' = .
	lab val vote_influenced`i' vote_options	
}

preserve	
	keep MP_id vote_id vote_natural
	ren MP_id MP_id_d
	ren vote_natural vote_natural_d
	tempfile vote_d 
	save `vote_d'
restore

forv i=$drawstart/$drawend {
	foreach x in U L {
		ren `x'_MP_idRI`i' MP_id_d
		merge m:1 MP_id_d vote_id using `vote_d', keep(1 3) nogen
		ren vote_natural_d `x'_voteRI`i'
		drop MP_id_d
	}
}


// Fill in natural vote for those that can't be influenced

forv i=$drawstart/$drawend {
	** No known neighbours' votes
	replace vote_influenced`i' = vote_natural if mi(U_voteRI`i') & mi(L_voteRI`i')

	** Neighbours with votes are all same party
	replace vote_influenced`i' = vote_natural if !(U_diffparty`i'==1 & !mi(U_voteRI`i')) & !(L_diffparty`i'==1 & !mi(L_voteRI`i'))

	** Already voting the same way as neighbours with votes
	replace vote_influenced`i' = vote_natural if (vote_natural==U_voteRI`i' & vote_natural==L_voteRI`i') | ///
											(vote_natural==U_voteRI`i' & mi(L_voteRI`i')) | ///
											(vote_natural==L_voteRI`i' & mi(U_voteRI`i'))
}


// Now randomly pick who talks to who, and whether influence happens
forv i=$drawstart/$drawend {
	g convo_with_L`i' = runiform()<=0.5
	la var convo_with_L`i' "=1 if conversation is between focal MP and L (when there is a choice)"

	g influence_happens`i' = runiform()<=$pcue
	la var influence_happens`i' "=1 if a conversation successfully influences"
	replace vote_influenced`i' = vote_natural if mi(vote_influenced`i') & influence_happens`i'==0
}


// Pick possible final influenced votes:
* (1) if influenced by U
* (2) if influenced by L
forv j=$drawstart/$drawend {
	foreach x in L U {
		forv i=0/3 {
			g `x'_inf_focal`j'_`i' = `i'
			
			* Rule out impossible votes
			replace `x'_inf_focal`j'_`i'=. if vote_natural==`x'_voteRI`j' & `x'_inf_focal`j'_`i'!=vote_natural
			replace `x'_inf_focal`j'_`i'=. if vote_natural>`x'_voteRI`j' & (`x'_inf_focal`j'_`i'<`x'_voteRI`j' | ///
																	`x'_inf_focal`j'_`i'>=vote_natural)
			replace `x'_inf_focal`j'_`i'=. if vote_natural<`x'_voteRI`j' & (`x'_inf_focal`j'_`i'>`x'_voteRI`j' | ///
																	`x'_inf_focal`j'_`i'<=vote_natural)
		}

		* Now pick one of the remaining options at random
		forv i=0/3 {
			g double randomswitch`x'`j'`i' = runiform()
		}
		rowranks randomswitch`x'`j'0 randomswitch`x'`j'1 randomswitch`x'`j'2 randomswitch`x'`j'3, ///
				g(switch`x'`j'0 switch`x'`j'1 switch`x'`j'2 switch`x'`j'3)
				
		g `x'_inf_focal`j'=.
		lab val `x'_inf_focal`j' vote_options
		forv i=0/3 {
			replace `x'_inf_focal`j' = `x'_inf_focal`j'_0 if switch`x'`j'`i'==1 & mi(`x'_inf_focal`j')
			replace `x'_inf_focal`j' = `x'_inf_focal`j'_1 if switch`x'`j'`i'==2 & mi(`x'_inf_focal`j')
			replace `x'_inf_focal`j' = `x'_inf_focal`j'_2 if switch`x'`j'`i'==3 & mi(`x'_inf_focal`j')
			replace `x'_inf_focal`j' = `x'_inf_focal`j'_3 if switch`x'`j'`i'==4 & mi(`x'_inf_focal`j')
		}
		assert !mi(`x'_inf_focal`j') if !(mi(`x'_inf_focal`j'_0) & mi(`x'_inf_focal`j'_1) & mi(`x'_inf_focal`j'_2) & mi(`x'_inf_focal`j'_3))
	}
}

forv i=$drawstart/$drawend {
	** Only L neighbour, influenced by L
	replace vote_influenced`i' = L_inf_focal`i' if mi(vote_influenced`i') & mi(U_inf_focal`i') & influence_happens`i'==1

	** Only U neighbour, influenced by U
	replace vote_influenced`i' = U_inf_focal`i' if mi(vote_influenced`i') & mi(L_inf_focal`i') & influence_happens`i'==1

	** Both neighbours, conversation with L, influenced
	replace vote_influenced`i' = L_inf_focal`i' if mi(vote_influenced`i') & convo_with_L`i'==1 & influence_happens`i'==1

	** Both neighbours, conversation with U, influenced
	replace vote_influenced`i' = U_inf_focal`i' if mi(vote_influenced`i') & convo_with_L`i'==0 & influence_happens`i'==1
		
	assert !mi(vote_influenced`i')
	decode vote_influenced`i', g(vote_inf`i')
	replace vote_inf`i'=lower(vote_inf`i')
	drop vote_influenced`i'
}

keep vote_id bill_id session_id MP_id vote_inf* vote_natural

** Save
save "$path_pch/Data/intermediate/Simulated/votes_simulated_RI${drawstart}to${drawend}_`pcuelab'", replace

timer off 1
timer list 1
beep